﻿using Ellanet.Windows;
using System;
using System.Diagnostics;
using System.Windows;

namespace Ellanet
{
    public partial class App
    {
        private void App_OnStartup(object sender, StartupEventArgs e)
        {
            ShutdownMode = ShutdownMode.OnExplicitShutdown;
            //ThreadPool.QueueUserWorkItem(GetAnimations);
            var sw = new SelectionWindow();
            sw.ShowDialog();
            Debug.WriteLine(sw.Vm.Activity);

            if (!sw.Vm.Activity.Equals(StaticCode.Activity.None))
            {
                var cd = new CountDownWindow(3, sw.Vm.Activity);
                cd.ShowDialog();
            }

            switch (sw.Vm.Activity)
            {
                case StaticCode.Activity.Test:
                    var tw = new TestWindow(sw.Vm.Sums, new TimeSpan(0, 0, sw.Vm.IntervalSeconds));
                    var twResult = tw.ShowDialog();

                    if (twResult.HasValue && twResult.Value)
                    {
                        var aw = new AnswersWindow(sw.Vm.Sums);
                        aw.ShowDialog();
                    }

                    break;
                case StaticCode.Activity.Practise:
                    var pw = new PractiseWindow(sw.Vm.Sums, new TimeSpan(0, 0, sw.Vm.IntervalSeconds));
                    var pwResult = pw.ShowDialog();

                    if (pwResult.HasValue && pwResult.Value)
                    {
                        var rw = new ResultsWindow(pw.Vm.CorrectAnswers, pw.Vm.TotalTime, pw.Vm.FastestAnswerTime, pw.Vm.FastestAnswer, pw.Vm.SlowestAnswerTime, pw.Vm.SlowestAnswer);
                        rw.ShowDialog();
                    }

                    break;
            }

            //Thread.Sleep(2000);
            //var rw = new ResultsWindow(12, TimeSpan.FromMilliseconds(65302), TimeSpan.FromMilliseconds(2341), new Sum() {Multiplicand = 10, Multiplier = 4}, TimeSpan.FromMilliseconds(6000), new Sum() { Multiplicand = 6, Multiplier = 4 });
            //rw.ShowDialog();

            Shutdown();
        }

        //private void GetAnimations(object stateInfo)
        //{
        //    try
        //    {
        //        var versionXmlDoc = new XmlDocument();
        //        versionXmlDoc.Load(String.Format("{0}{1}", StaticCode.AnimationsUrlPrefix, StaticCode.AnimationsXmlName));
        //        var animationNodes = versionXmlDoc.SelectNodes("animations/animation");

        //        if (animationNodes != null)
        //        {
        //            StaticCode.Animations = (from XmlNode node in animationNodes select node.InnerText).ToList();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.WriteLine(ex.Message);
        //    }
        //}
    }
}
